//
//  YPMyInvestCell.m
//  ChengHuiTong
//
//  Created by CHT-Technology on 2017/3/2.
//  Copyright © 2017年 张文权. All rights reserved.
//

#import "YPMyInvestCell.h"
#import "NSDate+Extension.h"

@interface YPMyInvestCell (){
    
    UILabel *_titleLabel;
}

@end

@implementation YPMyInvestCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.selectionStyle = 0;
        _titleLabel = [UILabel creatWithFrame:CGRectMake(leftMargin, 10, 15*17, 20)
                                         font:[UIFont systemFontOfSize:autoScaleW(15)]
                                         text:@"幕墙玻璃工程公司资金周转（到期还本第3标）"
                                    textColor:[UIColor blackColor]
                                textAlignment:0];
        [self.contentView addSubview:_titleLabel];
        
        [self addLabelWithReuseIdentifier:reuseIdentifier];

    }
    
    return self;
}

- (void)addLabelWithReuseIdentifier:(NSString *)reuseIdentifier{
    
    NSInteger count = [reuseIdentifier isEqualToString:@"已结束"]?7:([reuseIdentifier isEqualToString:@"已转让"]?5:6);
    CGFloat contentW = SCREEN_WIDTH - leftMargin*2;
    for (int i = 0; i < count; i ++) {
        
        UILabel *label = [UILabel creatWithFrame:CGRectMake(leftMargin + (i%2)*contentW/2.f, _titleLabel.bottom + 5 + (i/2)*23, contentW/2.f, 23)
                                            font:[UIFont systemFontOfSize:autoScaleW(12)]
                                            text:@"债券价值"
                                       textColor:kColor_greyAshTitleColor
                                   textAlignment:0];
        label.tag = 1000+i;
        [self.contentView addSubview:label];
    }
}

#pragma mark -- 设置模型
- (void)setModel:(YPMyInvestModel *)model{
    
    _model = model;
    
    _titleLabel.text =_model.title;
    
    [self refreshMiddleViewsWithTextArray:[self getTextArray]];
    
}

- (NSArray *)getTextArray{
    
    NSMutableArray *textArray = [NSMutableArray array];
    NSString *timeLimit = [NSString stringWithFormat:@"%@个月",_model.time_limit];
    [textArray addObject:[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                       originalString:[NSString stringWithFormat:@"参考年化收益率 :  %@%%",_model.apr]
                                                         changeString:_model.apr]];
    if (_model.transfer_time.length > 0) { //已转让
        
        [textArray addObject:[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                           originalString:[NSString stringWithFormat:@"投资期限 :  %@",timeLimit]
                                                             changeString:timeLimit]];
        
        [textArray addObject:[self getAttributedStringWithChangeTextColor:kColor_AppNavColor
                                                           originalString:[NSString stringWithFormat:@"投资金额 :  %@元",_model.capital]
                                                             changeString:_model.capital]];
        
        [textArray addObject:[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                           originalString:[NSString stringWithFormat:@"投资时间 :  %@",[NSDate getDateWithInterval:[_model.addtime doubleValue] formart:@"yyyy-MM-dd"]]
                                                             changeString:[NSDate getDateWithInterval:[_model.addtime doubleValue] formart:@"yyyy-MM-dd"]]];
        
        [textArray addObject:[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                           originalString:[NSString stringWithFormat:@"转让时间 :  %@",[NSDate getDateWithInterval:[_model.transfer_time doubleValue] formart:@"yyyy-MM-dd"]]
                                                             changeString:[NSDate getDateWithInterval:[_model.transfer_time doubleValue] formart:@"yyyy-MM-dd"]]];
        
    }else{
        [textArray addObject:[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                           originalString:[NSString stringWithFormat:@"投资金额 :  %@元",_model.capital]
                                                             changeString:_model.capital]];
        
        
        [textArray addObject:[self getAttributedStringWithChangeTextColor:kColor_AppNavColor
                                                           originalString:[NSString stringWithFormat:@"应收本息 :  %@",_model.income.length > 0?_model.income:@"满标后计息"]
                                                             changeString:_model.income.length > 0?_model.income:@"满标后计息"]];
        
        [textArray addObject:[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                           originalString:[NSString stringWithFormat:@"投资期限 :  %@",timeLimit]
                                                             changeString:timeLimit]];
        
        NSString *style = YPGetRepayments(_model.style);
        
        [textArray addObject:[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                           originalString:[NSString stringWithFormat:@"还款方式 :  %@",style]
                                                             changeString:style]];
        
        [textArray addObject:[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                           originalString:[NSString stringWithFormat:@"投资时间 :  %@",[NSDate getDateWithInterval:[_model.addtime doubleValue] formart:@"yyyy-MM-dd"]]
                                                             changeString:[NSDate getDateWithInterval:[_model.addtime doubleValue] formart:@"yyyy-MM-dd"]]];
        
        if (_model.endtime.length > 0) {
            [textArray addObject:[self getAttributedStringWithChangeTextColor:[UIColor blackColor]
                                                              originalString:[NSString stringWithFormat:@"到期时间 :  %@",[NSDate getDateWithInterval:[_model.endtime doubleValue] formart:@"yyyy-MM-dd"]]
                                                                changeString:[NSDate getDateWithInterval:[_model.endtime doubleValue] formart:@"yyyy-MM-dd"]]];
        }
    }
    

    return textArray;
}


- (NSAttributedString *)getAttributedStringWithChangeTextColor:(UIColor *)textColor
                                                originalString:(NSString *)originalString
                                                  changeString:(NSString *)changeString
{
    
    NSMutableAttributedString *mAttString = [[NSMutableAttributedString alloc]initWithString:originalString];
    [mAttString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:autoScaleW(13)],
                                NSForegroundColorAttributeName:textColor,
                                } range:[originalString rangeOfString:changeString]];
    if ([originalString rangeOfString:@"元"].location != NSNotFound) {
        [mAttString addAttribute:NSForegroundColorAttributeName value:textColor range:[originalString rangeOfString:@"元"]];
        
    }
    if ([originalString rangeOfString:@"%"].location != NSNotFound) {
        [mAttString addAttribute:NSForegroundColorAttributeName value:textColor range:[originalString rangeOfString:@"%"]];
        
    }
    return mAttString;
}


- (void)refreshMiddleViewsWithTextArray:(NSArray *)textArray{
    
    [textArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *label = [self.contentView viewWithTag:idx+1000];
        label.attributedText = obj;
    }];
}

- (void)setFrame:(CGRect)frame{
    
    frame.origin.y += 10;
    frame.size.height -=10;
    
    return [super setFrame:frame];
}

@end
